//
//  KFDDetailedDiagramViewModel.m
//  KFD
//
//  Created by JIRENTIANXIANG on 7/3/15.
//  Copyright © 2015 kfd. All rights reserved.
//

#import "KFDDetailedDiagramViewModel.h"
#import "KFDMarketPrice.h"
#define TYPE_COUNT 6

@interface KFDDetailedDiagramViewModel ()

@property (assign,atomic) BOOL loading;

@end

@implementation KFDDetailedDiagramViewModel

-(instancetype)init
{
    if (self=[super init]) {
        self.loading=NO;
        self.diagramPoints5Minutes=[[KFDDiagramPoints alloc] init];
        self.diagramPoints15Minutes=[[KFDDiagramPoints alloc] init];
        self.diagramPoints30Minutes=[[KFDDiagramPoints alloc] init];
        self.diagramPoints1Hour=[[KFDDiagramPoints alloc] init];
    }
    return self;
}

-(void)loadDetailedMarketPrice
{
    if (self.marketPrices.count==0) {
        return;
    }
    KFDMarketPrice* marketPrice=self.marketPrices[self.currentPriceIndex];
    KFDClient* client=[KFDClient sharedClient];
    [client getDetailedMarketPriceOfType:marketPrice.typename successHandler:^(KFDDetailedMarketPrice *price) {
        self.detailedMarketPrice=price;
    } failureHandler:^(KFDResponseBody *response) {
        
    }];
}

-(void)loadDiagramPointsWithPeriod:(KFDDiagramDataPeriod)period completionHandler:(KFDViewModelLoadDataCompletionHandler)completionHandler
{
    KFDClient* client=[KFDClient sharedClient];
    if (self.marketPrices.count==0) {
        return;
    }
    KFDMarketPrice* marketPrice=self.marketPrices[self.currentPriceIndex];
    [client getDiagramDataWithType:marketPrice.typename period:period beforeTime:0 successHandler:^(NSArray *diagramPoints) {
        switch (period) {
            case KFDDiagramDataPeriod5Minutes:
                self.diagramPoints5Minutes.diagramPoints=diagramPoints;
                self.diagramPoints5Minutes.leftPointShown=0;
                self.diagramPoints5Minutes.numberOfPointsShown=diagramPoints.count;
                break;
            case KFDDiagramDataPeriod15Minutes:
                self.diagramPoints15Minutes.diagramPoints=diagramPoints;
                self.diagramPoints15Minutes.leftPointShown=0;
                self.diagramPoints15Minutes.numberOfPointsShown=diagramPoints.count;
                break;
            case KFDDiagramDataPeriod30Minutes:
                self.diagramPoints30Minutes.diagramPoints=diagramPoints;
                self.diagramPoints30Minutes.leftPointShown=0;
                self.diagramPoints30Minutes.numberOfPointsShown=diagramPoints.count;
                break;
            case KFDDiagramDataPeriod1Hour:
                self.diagramPoints1Hour.diagramPoints=diagramPoints;
                self.diagramPoints1Hour.leftPointShown=0;
                self.diagramPoints1Hour.numberOfPointsShown=diagramPoints.count;
                break;
            default:
                break;
        }
        
        if (completionHandler) {
            completionHandler(YES);
        }
    } failureHandler:^(KFDResponseBody *response) {
        if (completionHandler) {
            completionHandler(NO);
        }
    }];
}

-(BOOL)loadLeftPointOfPeriodType:(KFDDiagramDataPeriod)period
{
    KFDDiagramPoints* dp;
    switch (period) {
        case KFDDiagramDataPeriod5Minutes:
            dp=self.diagramPoints5Minutes;
            break;
        case KFDDiagramDataPeriod15Minutes:
            dp=self.diagramPoints15Minutes;
            break;
        case KFDDiagramDataPeriod30Minutes:
            dp=self.diagramPoints30Minutes;
            break;
        case KFDDiagramDataPeriod1Hour:
            dp=self.diagramPoints1Hour;
            break;
        default:
            break;
    }
    if (dp.leftPointShown==0) {
        if (!self.loading) {
            self.loading=YES;
            KFDClient* client=[KFDClient sharedClient];
            KFDMarketPrice* marketPrice=self.marketPrices[self.currentPriceIndex];
            KFDDiagramPoint* diagramPoint;
            diagramPoint=dp.diagramPoints.firstObject;
            [client getDiagramDataWithType:marketPrice.typename period:period beforeTime:@(diagramPoint.date.timeIntervalSince1970).integerValue successHandler:^(NSArray *diagramPoints) {
                dp.diagramPoints=[diagramPoints arrayByAddingObjectsFromArray:dp.diagramPoints];
                dp.leftPointShown+=dp.numberOfPointsShown;
                self.loading=NO;
            } failureHandler:^(KFDResponseBody *response) {
                
            }];
        }
        return YES;
    }else{
        dp.leftPointShown-=1;
        [[NSNotificationCenter defaultCenter] postNotificationName:KFDDetailedDiagramViewShouldUpdateNotification object:nil];
        return NO;
    }
}



@end
